package com.situ.dao;

import com.situ.util.DBUtil;
import com.situ.vo.StatisticsVO;
import java.util.List;

public class StatisticsDao {

    public List<StatisticsVO> all(int index, int limit, String keyword) {
        if (keyword == null || "".equals(keyword)) {
           return null;
        }

        if (keyword != "null" && !"".equals(keyword)) {
            String[] arr = keyword.split("~");
            String k1=arr[0].trim();
            String k2=arr[1].trim();
            if(k1.length()==7){
                k1+="-01";
                k2+="-01";
            }
            if(k1.length()==4){
                k1+="-01-01";
                k2+="-01-01";
            }
            String sql="select danju.comm_id,commodity.comm_name,danju.dj_crk,commodity.comm_manufacturer,commodity.comm_marking,commodity.comm_specifications,sum(danju.dj_quantity) dj_quantity from danju inner join commodity on commodity.comm_id=danju.comm_id where danju.dj_date between '" + k1 + "' and '" + k2 + "' GROUP BY danju.comm_id,danju.dj_crk limit ?,?";
            return DBUtil.executeDQL(sql, StatisticsVO.class,index, limit);
        }
        return null;
    }

    public Long count(String keyword) {
        if (keyword == null || "".equals(keyword)) {
            
            return null;
        }
        String[] arr = keyword.split("~");
        String k1=arr[0].trim();
        String k2=arr[1].trim();
        if(k1.length()==7){
            k1+="-01";
            k2+="-01";
        }
        if(k1.length()==4){
            k1+="-01-01";
            k2+="-01-01";
        }
        String sql = "select count(1) from (select count(*) from danju inner join commodity on commodity.comm_id=danju.comm_id where danju.dj_date between '" + k1 + "' and '" + k2 + "' GROUP BY danju.comm_id,danju.dj_crk) as aaa ";
        return DBUtil.executeCount(sql);
    }
}
